<?php
header("Content-Type: text/html; charset=utf-8");
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	$itemid = $_POST['itemid'];
	$_key = $_POST["key"];
	$idList = implode("", $itemid);
	$_key2 = sha1('goods'.sha1($idList.'~!@#$%^'));
	if ($_key == $_key2){
		
		$itemurl = $_POST['itemurl'];
		$itemtitle = $_POST['itemtitle'];
		$itempriceold = $_POST['itempriceold'];
		$itempricenew = $_POST['itempricenew'];
		$itemdesc = $_POST['itemdesc'];
		$itemdate = $_POST['itemdate'];
		$itemsort = $_POST['itemsort'];
		$itemimgs = $_POST['itemimgs'];
		$itemcate = $_POST['itemcate'];
		$itemsalednum = $_POST['itemsalednum'];
		$itemcommission = $_POST['itemcommission'];
		$itemcommission2 = $_POST['itemcommission2']?$_POST['itemcommission2']:0;
		$pic_2 = $_POST["pic_2"];
		//   .....
		if ($_POST['type']=='9point9' || $_POST['type']=='19point9'){
			$itemStartTime = $_POST['itemStartTime'];
			$itemEndTime = $_POST['itemEndTime'];
		}
		$status = 1;
		$itemsArray=array();
		foreach ($itemid as $key => $item){
			$itempic[$key]= $_POST['itempic'.$itemid[$key]][0];
			if( (empty($itempic[$key]) && empty($pic_2[$key])) || empty($itemid[$key]) || empty($itemurl[$key]) || empty($itemtitle[$key]) || empty($itempriceold[$key]) || empty($itempricenew[$key]) || empty($itemdesc[$key]) || empty($itemdate[$key]) || empty($itemsort[$key]) || empty($itemcate[$key]) || empty($itemsalednum[$key]) || empty($itemcommission[$key]) ){
				$itemsArray[$key]['isEmptyWord'] = '';
				if (empty($itempic[$key]) && empty($pic_2[$key])){
					$itemsArray[$key]['isEmptyWord'] .= "图片为空|";}
					if (empty($itemid[$key])){$itemsArray[$key]['isEmptyWord'] .= "id为空|";}
					if (empty($itemurl[$key])){$itemsArray[$key]['isEmptyWord'] .= "地址为空|";}
					if (empty($itemtitle[$key])){$itemsArray[$key]['isEmptyWord'] .= "标题为空|";}
					if (empty($itempriceold[$key])){$itemsArray[$key]['isEmptyWord'] .= "原价为空|";}
					if (empty($itempricenew[$key])){$itemsArray[$key]['isEmptyWord'] .= "现价为空|";}
					if (empty($itemdesc[$key])){$itemsArray[$key]['isEmptyWord'] .= "推荐为空|";}
					if (empty($itemdate[$key])){$itemsArray[$key]['isEmptyWord'] .= "发布时间为空|";}
					if (empty($itemsort[$key])){$itemsArray[$key]['isEmptyWord'] .= "排序为空|";}
					if (empty($itemcate[$key])){$itemsArray[$key]['isEmptyWord'] .= "分类为空|";}
					if (empty($itemsalednum[$key])){$itemsArray[$key]['isEmptyWord'] .= "销售数量为空|";}
					if (empty($itemcommission[$key])){$itemsArray[$key]['isEmptyWord'] .= "佣金为空|";}
					$status=0;
					//0:为空，1:不为空
					$itemsArray[$key]['isEmpty'] = 1;
			}
			//
			if ($_POST['type']=='9point9' || $_POST['type']=='19point9'){
				if(empty($itemStartTime[$key]) || empty($itemEndTime[$key])){
					if (empty($itemStartTime[$key])){$itemsArray[$key]['isEmptyWord'] .= "开始时间|";}
					if (empty($itemEndTime[$key])){$itemsArray[$key]['isEmptyWord'] .= "结束时间|";}
					$status=0;
					//0:为空，1:不为空
					$itemsArray[$key]['isEmpty'] = 1;
				}
			}
				
				
			$itemsArray[$key]['itemid']=$itemid[$key];
			$itemsArray[$key]['itemurl']=$itemurl[$key];
			$itemsArray[$key]['itemtitle']=$itemtitle[$key];
			$itemsArray[$key]['itempriceold']=$itempriceold[$key];
			$itemsArray[$key]['itempricenew']=$itempricenew[$key];
			$itemsArray[$key]['itemdesc']=$itemdesc[$key];
			$itemsArray[$key]['itemdate']=$itemdate[$key];
			$itemsArray[$key]['itemsort']=$itemsort[$key];
			$itemsArray[$key]['itempic']= !empty($pic_2[$key])?$pic_2[$key]:$itempic[$key];
			$itemsArray[$key]['item_imgs']=json_decode(stripslashes($itemimgs[$key]));
			$itemsArray[$key]['itemcate']=$itemcate[$key];
			$itemsArray[$key]['itemsalednum']=$itemsalednum[$key];
			$itemsArray[$key]['itemcommission']=$itemcommission[$key];
			$itemsArray[$key]['itemcommission2']=$itemcommission2[$key];
			//
			if ($_POST['type']=='9point9' || $_POST['type']=='19point9'){
				$itemsArray[$key]['itemStartTime']=$itemStartTime[$key];
				$itemsArray[$key]['itemEndTime']  =$itemEndTime[$key];
		
			}
		}
		
		
		/* foreach ($itemid as $key=>$item){
		 	
		$itemsArray[$key]['itemid']=$itemid[$key];
		$itemsArray[$key]['itemurl']=$itemurl[$key];
		$itemsArray[$key]['itemtitle']=$itemtitle[$key];
		$itemsArray[$key]['itempriceold']=$itempriceold[$key];
		$itemsArray[$key]['itempricenew']=$itempricenew[$key];
		$itemsArray[$key]['itemdesc']=$itemdesc[$key];
		$itemsArray[$key]['itemdate']=$itemdate[$key];
		$itemsArray[$key]['itemsort']=$itemsort[$key];
		$itemsArray[$key]['itempic']=$itempic[$key];
		$itemsArray[$key]['item_imgs']=json_decode(stripslashes($itemimgs[$key]));
		$itemsArray[$key]['itemcate']=$itemcate[$key];
		$itemsArray[$key]['itemsalednum']=$itemsalednum[$key];
		//var_dump($itemsArray[$key]['item_imgs']);
		//echo $key."<br />"."<br />";
			
		} */
		if($status==0 ){
			//echo "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />";
			foreach ($itemsArray as $key => $item){
				echo ($key+1).'.'.$item['isEmptyWord']."<br />";
			}
			//echo "<br /><input type='button' value='后退' onclick='window.history.back()' />";
		}else{
			require_once '../baseClasses/config_ser.php';
			//require_once '../baseClasses/config_loc.php';
			require_once '../baseClasses/dbConn.php';
			$db = new dbConn();
			//if ($_POST['type']=='9point9'){
				//$cateid = 0;
				foreach ($itemsArray as $key => $item){
					echo ($key+1).".";
					//if(strtotime($item['itemdate'])>=strtotime(date("Y-m-d"))){
						/* $num=goodsidexist($db,$item['itemdate'],$item['itemid'],$_POST['updater']);
						$noAccess=goodsisexist15day($db,$item['itemid'],$_POST['updater'],$item['itemdate']);
						$noAccess2=goodsForOne($db,$item['itemid'],$_POST['updater']);
						if ($num<=0 && $noAccess<=0 && $noAccess2<=0){ */
							if ($_POST['type']=='9point9' || $_POST['type']=='19point9'){
							    if($_POST['type']=='19point9'){ $insertT = 'priceninetenpointnine';}
							    if($_POST['type']=='9point9') { $insertT = 'priceninepointnine';}
								$db->sql="INSERT INTO {$insertT}(publishDate,picture,title,goodsUrl,`desc`,originalPrice,currentPrice,`status`,sort,comefrom,updater,cateID,saledNum,commission,startTime,endTime,goodsID,commission2) ".
									"VALUES('".$item['itemdate']."','".$item['itempic']."','".$item['itemtitle']."','".$item['itemurl']."','".$item['itemdesc']."',".$item['itempriceold'].",".$item['itempricenew'].",0,".$item['itemsort'].",2,'".$_POST['updater']."',".$item['itemcate'].",".$item['itemsalednum'].",".$item['itemcommission'].",".strtotime($item['itemStartTime']).",".strtotime($item['itemEndTime']).",'".$item['itemid']."','".$item['commission2']."')";
							}else{
								$cateid = getCateId($db, $item['itemcate']);
								$db->sql="INSERT INTO content(publishDate,picture,title,goodsUrl,`desc`,originalPrice,currentPrice,`status`,sort,comefrom,updater,cateID,saledNum,commission,goodsID,commission2) ".
									"VALUES('".$item['itemdate']."','".$item['itempic']."','".$item['itemtitle']."','".$item['itemurl']."','".$item['itemdesc']."',".$item['itempriceold'].",".$item['itempricenew'].",0,".$item['itemsort'].",2,'".$_POST['updater']."',".$cateid.",".$item['itemsalednum'].",".$item['itemcommission'].",'".$item['itemid']."','".$item['commission2']."')";
							}
							$db->doExecute();
							echo "添加成功";
						/* }else{
							echo "<span style='color:red;'>添加失败";
							if ($num>0) {
								echo "本月已有编辑添加该商品。";
							}
							if ($noAccess==1) {
								echo "在这之前15天已有添加该商品。";
							}
							if ($noAccess==2) {
								echo "在这之后15天已有添加该商品。";
							}
							if ($noAccess2>0) {
								echo "已被其他责任编辑选用该商品。";
							}
							echo "</span>";
						} */
					/* }else{
						echo "<span style='color:red;'>添加失败。只能添加今天或者之后的商品，不能添加之前的！</span>";
					} */
					echo "<br />";
				}
			//}else{
			/* 	foreach ($itemsArray as $key => $item){
					$num=goodsidexist($db,$item['itemdate'],$item['itemid'],$_POST['updater']);
					$cateid = getCateId($db, $item['itemcate']);
					$db->sql="INSERT INTO content(publishDate,picture,title,goodsUrl,`desc`,originalPrice,currentPrice,`status`,sort,comefrom,updater,cateID,saledNum,commission,goodsID) ".
						"VALUES('".$item['itemdate']."','".$item['itempic']."','".$item['itemtitle']."','".$item['itemurl']."','".$item['itemdesc']."',".$item['itempriceold'].",".$item['itempricenew'].",0,".$item['itemsort'].",2,'".$_POST['updater']."',".$cateid.",".$item['itemsalednum'].",".$item['itemcommission'].",'".$item['itemid']."')";
					$db->doExecute();
				}
			} */
			echo "<br />请在<a href='http://app100656353.qzone.qzoneapp.com/admin'>优品购后台</a>查看成功添加商品并审核通过";
		}
	}else{
		echo "error...";
	}
}else{
	echo "错误error......";
}

function getCateId($db,$cate){
	$db->sql = "SELECT id FROM category WHERE cate = '".trim($cate)."'";
	return $db->doResultOne();
}
function goodsidexist($db,$date,$goodsid,$updater){
	$num = 0;
	$year = date('Y',strtotime($date));
	$month=date('n',strtotime($date));
	$db->sql = "SELECT count(id) num FROM content WHERE goodsID='{$goodsid}' AND YEAR(publishDate)={$year} AND MONTH(publishDate)={$month} AND updater != '{$updater}' AND isDel=0 UNION ".
			   "SELECT count(id) num FROM priceninepointnine WHERE goodsID='{$goodsid}' AND YEAR(publishDate)={$year} AND MONTH(publishDate)={$month} AND updater != '{$updater}' AND isDel=0";
	$numlist = $db->doResultList2();
	foreach ($numlist as $i){
		//var_dump($i)
		$num+=$i['num'];
	}
	return $num;
}
//商品归属
function goodsForOne($db,$goodsid,$updater){
	$db->sql="SELECT COUNT(c.id) num FROM content c RIGHT JOIN admin a ON c.updater=a.userName AND a.isActive=1 WHERE c.publishDate >= '2013-04-01' AND c.goodsID = '{$goodsid}' AND c.updater != '{$updater}' UNION ".
			 "SELECT COUNT(c.id) num FROM priceninepointnine c RIGHT JOIN admin a ON c.updater=a.userName AND a.isActive=1 WHERE c.publishDate >= '2013-04-01' AND c.goodsID = '{$goodsid}' AND c.updater != '{$updater}'";
	$goodsInfoTongJi2 = $db->doResultList2();
	foreach ($goodsInfoTongJi2 as $i){
		//var_dump($i)
		$num+=$i['num'];
	}
	$noAccess2=0;
	if ($num>0) {
		$noAccess2=1;
	}
	return $noAccess2;
}
function goodsisexist15day($db,$goodsid,$updater,$publishDate){
	$db->sql="SELECT count(id) cid,IF( (publishDate>=DATE(DATE_SUB('{$publishDate}', INTERVAL 15 DAY)) AND (publishDate<='{$publishDate}') ) ,1,0) cLT,IF( (publishDate<=DATE(DATE_ADD('{$publishDate}', INTERVAL 15 DAY)) AND (publishDate>'{$publishDate}') ),1,0) cGT FROM content WHERE goodsID = {$goodsid} AND publishDate>=DATE(DATE_SUB('{$publishDate}', INTERVAL 15 DAY)) AND publishDate<=DATE(DATE_ADD('{$publishDate}', INTERVAL 15 DAY)) AND updater != '{$updater}' AND isDel!=1";
	//$rs = $db->doResult2();
	$goodsInfoTongJi = $db->doResult2();
		//var_dump($goodsInfoTongJi);
		$noAccess=0;
		if ($goodsInfoTongJi['cid']>0) {
			if ($goodsInfoTongJi['cLT']>0) {
				$noAccess=1;
			}
			if ($goodsInfoTongJi['cGT']>0) {
				$noAccess=2;
			}
		}
	return $noAccess;
}
?>